<template>
  <div class="login_container">
    <!-- 登录盒子 -->
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 登录表单区域 -->
      <el-form class="form_box" :model="loginForm" ref="loginBoxRef" :rules="rules">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" prefix-icon="iconfont icon-3702mima"></el-input>
        </el-form-item>
        <!-- 底部按钮 -->
        <el-form-item>
          <div class="btns">
            <el-button type="primary" @click="login">登录</el-button>
            <el-button type="info" @click="resetForm">重置</el-button>
          </div>
        </el-form-item>
      </el-form>

    </div>

  </div>
</template>
<script>
export default {
  data () {
    return {
      // 登录表单
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetForm () {
      // console.log(this)
      // 重置表单
      this.$refs.loginBoxRef.resetFields()
    },
    login () {
      // 登陆时对表单进行预验证
      this.$refs.loginBoxRef.validate(async valid => {
        console.log(valid)
        if (!valid) return
        // 如果某个方法返回值是Promise  可以用await简化
        // const result = await this.$http.post('login', this.loginForm)
        // 解构赋值 重命名
        const { data: res } = await this.$http.post('login', this.loginForm)
        console.log(res)
        if (res.meta.status !== 200) {
          this.$message.error('登录失败!')
        } else {
          this.$message.success('登录成功!')
          // 存储登录成功后的token   sessionStorage是会话存储
          window.sessionStorage.setItem('token', res.data.token)
          // 登录成功后路由跳转  编程式导航
          this.$router.push('/home')
        }
      })
    }
  }
}
</script>>
<style lang="less" scope>
.login_container {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
  .login_box {
    width: 450px;
    height: 300px;
    border-radius: 3px;
    background-color: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    // 头像
    .avatar_box {
      width: 130px;
      height: 130px;
      border: 1px solid #fff;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #eee;
      position: absolute;
      background-color: #fff;
      left: 50%;
      transform: translate(-50%, -50%);
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
    // 登录表单
    .form_box {
      margin-top:100px;
      padding: 0 20px;
      box-sizing: border-box;
      // 底部按钮
      .btns {
        display: flex;
        justify-content: flex-end;
      }
    }

  }
}
</style>
